<template>
    <div>
        <el-card>
            <span slot="header">商品列表</span>
            <el-form inline size="mini" :model="searchForm">
                <el-form-item label="商品名称">
                    <el-input  v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="商品分类">
                    <el-select v-model="searchForm.category" clearable>
                        <el-option v-for="val in priceNameArr" :key="val" :value="val" :label="val"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item >
                    <el-button @click="searchHandle" type="warning" icon="el-icon-search"></el-button>
                </el-form-item>
            </el-form>

            <el-table :data="tableData">
                <el-table-column type="expand">
                    <template slot-scope="props">
                        <el-form label-position="left" class="demo-table-expand">
                            <el-form-item label="商品名称">
                                <span>{{ props.row.name }}</span>
                            </el-form-item>
                            
                            <el-form-item label="商品 ID">
                                <span>{{ props.row.id }}</span>
                            </el-form-item>
                            
                            <el-form-item label="商品分类">
                                <span>{{ props.row.category }}</span>
                            </el-form-item>
                            <el-form-item label="商品价格">
                                <span>{{ props.row.price.toFixed(2) }}￥</span>
                            </el-form-item>
                            <el-form-item label="商品图片">
                               <img :src="props.row.imgUrl " alt="" width="80px">
                            </el-form-item>
                            <el-form-item label="商品描述">
                                <span>{{ props.row.goodsDesc }}</span>
                            </el-form-item>
                        </el-form>
                    </template>
                </el-table-column>
                <el-table-column label="商品 ID" prop="id">
                </el-table-column>
                <el-table-column label="商品名称" prop="name">
                </el-table-column>
                


                <el-table-column label="商品名称" prop="name"></el-table-column>
                <el-table-column label="所属分类" prop="category"></el-table-column>
                <el-table-column label="商品价格" prop="price"></el-table-column>
                <el-table-column label="商品图片" prop="imgUrl">
                    <template slot-scope="scope">
                        <img :src="scope.row.imgUrl" alt="" style="width:80px ">
                    </template>
                </el-table-column>
                <el-table-column label="商品描述" prop="goodsDesc"></el-table-column>
                <el-table-column label="操作" width="240">
                    <template slot-scope="scope">
                        <el-button size="small" @click="editGoodsHandle(scope.row)">编辑</el-button>
                        <el-button size="small" @click="deleteHandle(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 分页 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            background :page-sizes="[5, 10, 20, 50]" :pageSize="1" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <!-- 抽屉效果 -->
        <el-drawer title="修改商品" :visible.sync="drawer">
            <!-- 表单 -->
            <el-form :model="form" label-width="80px" style="width:400px" :rules="rules" ref="formRules">
                <el-form-item label="商品名称" prop="name">
                    <el-input v-model="form.name" clearable></el-input>
                </el-form-item>

                <el-form-item label="商品分类" prop="category">
                    <el-select v-model="form.category" style="width:100%">
                        <el-option v-for="val in priceNameArr" :key="val" :label="val" :value="val"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商品价格" prop="price">
                    <el-input-number v-model="form.price" :precision="2" :step="2" :min="0"></el-input-number>
                </el-form-item>

                <el-form-item label="商品图片" prop="imgUrl">
                    <el-upload class="avatar-uploader" :action="action" :show-file-list="false"
                        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="商品描述" prop="goodsDesc">
                    <el-input type="textarea" v-model="form.goodsDesc" maxlength="50" show-word-limit> </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="saveGoodsHandle">修改商品</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </div>
</template>

<script>
import { goodsList } from '@/api/goods'
export default {
   data() {
        return {
            //搜索表单
            searchForm: {
                name: "",
                category:""
            },
            //搜索数据
            searchParams:{
                name:"",
                category:""
            },
            action:"http://sell.h5.itsource.cn:8087/goods/goods_img_upload",
            //抽屉开关
            drawer: false,
            priceNameArr: [],
            tableData: [],
            //当前页数 
            currentPage: 1,
            pageSize: 5,
            total: 0,
            //修改商品的数据
            //表单数据
            form: {
                name: "",
                category: "",
                price: 0,
                imgUrl: "",
                goodsDesc: ""
            },
            //验证规则
            rules: {
                name: [
                    { required: true, message: "请输入商品名称", trigger: "blur" },
                    { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" }
                ],
                category: {
                    required: true,
                    message: "请选择商品分类",
                    trigger: "change"
                },
                price: {
                    required: true,
                    message: "请输入商品价格",
                    trigger: "blur"
                },
                imgUrl: {
                    required: true,
                    message: "请上传商品图片",
                    trigger: "blur"
                },
                goodsDesc: {
                    required: true,
                    message: "请输入商品描述",
                    trigger: "blur"
                }
            }
        }
    },
  methods: {
    //编辑
    handleEdit(index, row) {
      console.log(index, row);
    },
    //删除
    handleDelete(index, row) {
      console.log(index, row);
    },
    //当前条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    //当前页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    //获取列表
    async getData(){
      let currentPage =this.currentPage
      let pageSize = this.pageSize
      let res = await goodsList ({
        currentPage,
        pageSize,
        name:this.searchParams.name,
                category:this.searchParams.category,

      })
      console.log(res);
      let {data ,total} = res.data
      this.total=total
      this.tableData = data
    },
    
  },
  created(){
    this.getData()
  }
};
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
}
</style>